package sample.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import sample.service.MainService;
import sample.util.FileChooserSample;
import sample.util.ParamsUtil;
import sample.util.txt.FileOfTxtUtil;
import sample.util.txt.FileTxtParams;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    MainService mainService=new MainService();
    @FXML
    Label search;
    @FXML
    Label showTime;
    @FXML
    Button toolBtn_1;
    @FXML
    private TabPane tabnew;
    @FXML
    private Tab reportDB;
    @FXML
    private Tab mainSheet;
    @FXML
    private Tab mainView;
    @FXML
    TableView dataTableDB;
    @FXML
    TableView dataTableMain;
    @FXML
    TextField txtPath;
    @FXML
    TextField txtPathMianSheet;
    @FXML
    ComboBox sheet_index;
    @FXML
    ComboBox mainSheetSelect;
    @FXML
    Pane mainPane;
    String fileName;
    public void onAddDB(){
        tabnew.getTabs().remove(reportDB);
        tabnew.getTabs().add(reportDB);
    }
    public void onMainSheet(){
        tabnew.getTabs().remove(mainSheet);
        tabnew.getTabs().add(mainSheet);
    }
    public void onShowMainView(){
        tabnew.getTabs().remove(mainView);
        tabnew.getTabs().add(mainView);
    }
    public void onImport(){
        Stage stage=new Stage();
        if(null!=txtPath.getText()) {
            FileChooserSample.fileChooserDilog(stage,txtPath.getText().trim(),fileName);
        }else{
            FileChooserSample.fileFirstChooserDilog(stage);
        }
        fileName=FileChooserSample.fileName;
        txtPath.setText(FileChooserSample.filePath);
       try {
           mainService.initTable(dataTableDB, txtPath.getText().trim(), Integer.parseInt((String) sheet_index.getSelectionModel().getSelectedItem()));
       }catch(NullPointerException e){}
    }
    public void onClearDataTable(){
        ParamsUtil.data_db.clear();
        dataTableDB.setItems(ParamsUtil.data_db);
    }
    public void onClearMainSheet(){
        ParamsUtil.data_mainSheet.clear();
        dataTableMain.setItems(ParamsUtil.data_mainSheet);
    }
    public void onImportMainSheet(){
        Stage stage=new Stage();
        if(null!=txtPathMianSheet.getText()) {
            FileChooserSample.fileChooserDilog(stage,txtPathMianSheet.getText().trim(),fileName);
        }else{
            FileChooserSample.fileFirstChooserDilog(stage);
        }
        fileName=FileChooserSample.fileName;
        txtPathMianSheet.setText(FileChooserSample.filePath);
        try {
            mainService.initMainTable(dataTableMain, txtPathMianSheet.getText().trim(), Integer.parseInt((String) mainSheetSelect.getSelectionModel().getSelectedItem()));
        }catch(NullPointerException e){}
    }
    public void saveMainPath(){
        FileOfTxtUtil.creatFile(FileTxtParams.file);
        FileOfTxtUtil.writeDataToTxt(txtPath.getText().trim()+"#"+fileName,"path\\"+FileTxtParams.name);
    }
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        tabnew.getTabs().retainAll();
        tabnew.getTabs().add(mainView);
        mainPane.getStyleClass().add("pane1");
        System.out.println("--->"+FileOfTxtUtil.readDataToTxt("path\\"+FileTxtParams.name));
        String[] pathMain=(FileOfTxtUtil.readDataToTxt("path\\"+FileTxtParams.name)).split("#");
        try {
           if(null!=pathMain[1]) {
               txtPath.setText(pathMain[0].trim());
               fileName = pathMain[1].trim();
           }
        }catch(ArrayIndexOutOfBoundsException e){}
        search.setGraphic(new ImageView("/img/search.png"));
        sheet_index.getItems().addAll((Object[]) ParamsUtil.sheetIndexArr);
        sheet_index.getSelectionModel().select(1);
        mainSheetSelect.getItems().addAll((Object[]) ParamsUtil.sheetIndexArr);
        mainSheetSelect.getSelectionModel().select(1);
    }
}
